use actix_web::{AsyncResponder, FutureResponse, HttpMessage, HttpRequest, HttpResponse, Json, State};
use common::state::AppState;
use futures::Future;
use model::member::{Login, Register};

pub fn register((signup_user, state): (Json<Register>, State<AppState>)) -> FutureResponse<HttpResponse> {
    println!("{}", "path");

    state.db.send(Register {
        username: signup_user.username.clone(),
        email: signup_user.email.clone(),
        password: signup_user.password.clone(),
        confirm_password: signup_user.confirm_password.clone(),
    })
        .from_err()
        .and_then(|res| {
            match res {
                Ok(signup_msg) => Ok(HttpResponse::Ok().json(signup_msg)),
                Err(_) => Ok(HttpResponse::InternalServerError().into())
            }
        }).responder()
}


pub fn login((signup_user, state): (Json<Login>, State<AppState>)) -> FutureResponse<HttpResponse> {
    println!("{}", "login init");

    state.db.send(Login {
        username: signup_user.username.clone(),
        password: signup_user.password.clone(),
    })
        .from_err()
        .and_then(|res| {
            match res {
                Ok(signup_msg) => Ok(HttpResponse::Ok().json(signup_msg)),
                Err(_) => Ok(HttpResponse::InternalServerError().into())
            }
        }).responder()
}